<%@ page import="java.util.Iterator" %>
<%@ page language="java" %>
<%@ page pageEncoding="UTF-8" %>
<%@ page contentType="text/html;charset=UTF-8"  %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
    <head>
        <title>监听器</title>
        <style type="text/css">
            .wrapper { border: 1px solid blue ; margin: 15px ; padding: 5px }
            .wrapper p { background: #f1f1f1 ; color: blue ; margin: 0 ; }
            .wrapper form { margin: 5px 0 ; border: 1px solid #dfdfdf ; }
            .link-wrapper { display: flex ; border: 1px solid #dfdfdf ; }
            .link-wrapper>a { line-height: 24px ; margin: 5px 10px 5px 0 ;}
        </style>
    </head>
    <body>

        <div class="wrapper">
            <p>测试与生命周期有关的监听器</p>
            <div class="link-wrapper">
                <a href="${pageContext.request.contextPath}/session/create">创建会话</a>
                <a href="${pageContext.request.contextPath}/session/destroy">销毁会话</a>
            </div>
        </div>

        <div class="wrapper">
            <p>通过操作ServletCotnext中的属性测试与ServletCotnext属性操作有关的监听器</p>
            <form action="${pageContext.request.contextPath}/application/attribute" method="post">
                <input type="text" name="name" placeholder="属性名">
                <input type="text" name="value" placeholder="属性值">
                <button type="submit">提交</button>
            </form>
            <div class="link-wrapper">
                <%
                    Iterator<String> iterator = application.getAttributeNames().asIterator();
                    while( iterator.hasNext() ){
                        String attributeName = iterator.next();
                        if( !attributeName.startsWith( "javax" ) && !attributeName.startsWith( "org" ) ) {
                            pageContext.setAttribute( "name" , attributeName );
                            // out.println( "<a href=\"/application/attribute?name=" + name + "\">删除 " + name + "</a>" );
                        %>
                        <a href="${pageContext.request.contextPath}/application/attribute?name=${name}">删除${name}属性</a>
                    <%
                        }
                    }
                    %>
            </div>
        </div>

        <div class="wrapper">
            <p>通过操作HttpSession中的属性测试与HttpSession属性操作有关的监听器</p>
            <form action="${pageContext.request.contextPath}/session/attribute" method="post">
                <input type="text" name="name" placeholder="属性名">
                <input type="text" name="value" placeholder="属性值">
                <button type="submit">提交</button>
            </form>
            <div class="link-wrapper">
                <%  // 之前已经使用 Iterator<String> iterator 声明过 iterator
                    iterator = session.getAttributeNames().asIterator();
                    while( iterator.hasNext() ){
                        String attributeName = iterator.next();
                        pageContext.setAttribute( "name" , attributeName );
                %>
                    <a href="${pageContext.request.contextPath}/session/attribute?name=${name}">删除${name}属性</a>
                <% } %>
            </div>
        </div>

        <div class="wrapper">
            <p>通过操作ServletRequest中的属性测试与ServletRequest属性操作有关的监听器</p>
            <form action="${pageContext.request.contextPath}/request/attribute" method="post">
                <input type="text" name="name" placeholder="属性名">
                <input type="text" name="value" placeholder="属性值">
                <button type="submit">提交</button>
            </form>
        </div>

        <div class="wrapper">
            <p>测试 HttpSessionBindingListener </p>
            <%-- 在 EL 中访问 page 、request 、session 、application 四个作用域可以使用 xxxScope 形式 --%>
            <c:if test="${ not empty sessionScope.user }">
                <span>
                    欢迎 ${ sessionScope.user.loginName } ，你可以点击
                </span>
                <%-- 在页面上点击超链接时，默认都是 GET 请求 --%>
                <a href="${pageContext.request.contextPath}/user/logout">注销登录</a>
            </c:if>
            <c:if test="${ empty sessionScope.user }">
                <%-- 以 POST 方式提交表单 --%>
                <form action="${pageContext.request.contextPath}/user/login" method="post">
                    <input type="text" name="loginName" placeholder="登录名称">
                    <input type="password" name="password" placeholder="登录密码">
                    <button type="submit">登录</button>
                </form>
            </c:if>
        </div>

        <div class="wrapper">
            <p>测试 HttpSessionActivationListener </p>
            <c:set var="counter" value="${ sessionScope.counter + 1 }" scope="session"></c:set>
            <div>${ sessionScope.counter }</div>
        </div>

    </body>
</html>
